<?php

namespace app\store\controller;

use think\Db;

class Zwordtodb extends \think\Controller
{
    private $wxappid = 10016;
    private $daIdArr = array('A'=>1,'B'=>2,'C'=>3,'D'=>4);
    // 脚本地址测试  http://www.toywxmall.com/index.php?s=/store/zwordtodb/oldindex

    public function oldindex(){
        $paramArr = $this->request->param();
        $csvname = !empty($paramArr['csvname']) ? $paramArr['csvname'] : '';
        if($csvname==""){
            echo '没有传递csvname';
        }
        else{
            $fileName = $_SERVER['DOCUMENT_ROOT'].'/myscript/wordfiles/'.$csvname.'.CSV';
            $fileNameArr = explode('_', $csvname);
            //echo $fileName;
            $csvArr = $this->read_csv($fileName);
            foreach ($csvArr as $kkk => $vvv){
                $csvStrArr = explode('@', $vvv[0]);
                //1,11一年级上册
                $csvStrArr[] = $fileNameArr[1]; //w_star
                $csvStrArr[] = $fileNameArr[2]; //w_level
                if(isset($vvv[1])){
                    $csvStrArr[] = $vvv[1];     //w_unit
                }
                //print_r($csvStrArr); echo '<br>';
                $testArr[] = $csvStrArr;
            }
            //ddd($testArr);

            foreach ($testArr as $key => $value){
                //不可重复插入
                $star = $value[4];
                $level = $value[5];
                $res = Db::name('qa_word')->where(['w_word'=>$value[0], 'w_star'=>$star, 'w_level'=>$level])->find();
                if(!$res){
                    echo 'key:'.$key;
                    $inArr11['w_star'] = $star;
                    $inArr11['w_level'] = $level;
                    $inArr11['w_word'] = trim($value[0]);
                    $inArr11['w_cname'] = $value[1];
                    $hh = $inArr11['w_word'][0];
                    $inArr11['w_header'] = $hh;
                    $inArr11['w_type'] = $value[2];
                    $inArr11['w_type_en'] = $value[3];
                    $inArr11['w_unit'] = isset($value[6]) ? $value[6] : "";
                    $inArr11['w_speak'] = isset($value[7]) ? $value[7] : "";
                    $inArr11['w_length'] = strlen($inArr11['w_word']);
                    $inArr11['w_addtime'] = date('Y-m-d H:i:s',time());
                    //print_r($inArr11); exit;
                    $dbId = $boxId = Db::name('qa_word')->insertGetId($inArr11);
                    echo '*****indbok:'.$dbId.'<br>';
                }
            }
        }
    }

    /**将excel中的单词入库，适用于人工编辑的excel表格*/
    public function index(){
        $paramArr = $this->request->param();
        $csvname = !empty($paramArr['csvname']) ? $paramArr['csvname'] : '';
        if($csvname==""){
            echo '没有传递csvname';
        }
        else{
            $fileName = $_SERVER['DOCUMENT_ROOT'].'/myscript/wordfiles/'.$csvname.'.CSV';
            //echo $fileName;
            $testArr = $this->read_csv($fileName);

//            echo '<pre>';
//            print_r($testArr);

            foreach ($testArr as $key => $value){
                //不可重复插入
                $star = $value[4];
                $level = $value[5];
                $res = Db::name('qa_word')->where(['w_word'=>$value[0], 'w_star'=>$star, 'w_level'=>$level])->find();
                if(!$res){
                    $inArr11['w_star'] = $star;
                    $inArr11['w_level'] = $level;
                    $inArr11['w_word'] = trim($value[0]);
                    $inArr11['w_cname'] = $value[1];
                    $hh = $inArr11['w_word'][0];
                    $inArr11['w_header'] = $hh;
                    $inArr11['w_type'] = $value[2];
                    $inArr11['w_type_en'] = $value[3];
                    $inArr11['w_speak'] = isset($value[6]) ? $value[6] : "";
                    $inArr11['w_length'] = strlen($inArr11['w_word']);
                    $inArr11['w_addtime'] = date('Y-m-d H:i:s',time());
                    //print_r($inArr11); exit;
                    $dbId = $boxId = Db::name('qa_word')->insertGetId($inArr11);
                    echo $dbId.'<br>';
                }
            }
        }
    }

    /** 将单词库中的单词组成试卷，包括每个小单元*/
    public function creqa000(){
        $boxnum = !empty($paramArr['boxnum']) ? $paramArr['boxnum'] : 10;   //一共出多少套试卷
        $num = !empty($paramArr['num']) ? $paramArr['num'] : 5; //每套卷子多少个题
        $price = !empty($paramArr['price']) ? $paramArr['price'] : '1';
        $des = !empty($paramArr['des']) ? $paramArr['des'] : '';    //试卷的描述
        $star = !empty($paramArr['star']) ? $paramArr['star'] : 1;

//        $title = !empty($paramArr['title']) ? $paramArr['title'] : '六年级英语下册-单词问答';
//        $type1 = !empty($paramArr['type1']) ? $paramArr['type1'] : '105';
//        $type2 = !empty($paramArr['type2']) ? $paramArr['type2'] : '115';

//        $level = !empty($paramArr['level']) ? $paramArr['level'] : 62;
//        $unit = !empty($paramArr['unit']) ? $paramArr['unit'] : 1;

        // 一年级英语所有试卷距离子
        // http://admin.ndlexue.com/index.php?s=/store/zwordtodb/creqa000
        $aqInfoArr = array(
            0=>array('六年级上册1单元单词问答', 105, 176, 61, 1),
            1=>array('六年级上册2单元单词问答', 105, 177, 61, 2),
            2=>array('六年级上册3单元单词问答', 105, 178, 61, 3),
            3=>array('六年级上册4单元单词问答', 105, 179, 61, 4),
            4=>array('六年级上册5单元单词问答', 105, 180, 61, 5),
            5=>array('六年级上册6单元单词问答', 105, 181, 61, 6),

            6=>array('六年级下册1单元单词问答', 105, 182, 62, 1),
            7=>array('六年级下册2单元单词问答', 105, 183, 62, 2),
            8=>array('六年级下册3单元单词问答', 105, 184, 62, 3),
            9=>array('六年级下册4单元单词问答', 105, 185, 62, 4),
        );

        //确定正式开始录入试卷
        echo '确定生成试卷，请注销exit语句';
        exit;

        foreach($aqInfoArr as $val){
            $title = $val[0];
            $type1 = $val[1];
            $type2 = $val[2];
            $level = $val[3];
            $unit = $val[4];
            $this->createqa($boxnum,$num,$price,$des, $title,$type1,$type2,$star,$level, $unit);
        }
    }
    /**将单词库中的单子组成试卷*/
    public function createqa($boxnum,$num,$price,$des, $title,$type1,$type2,$star,$level, $unit){
//        $boxnum = !empty($paramArr['boxnum']) ? $paramArr['boxnum'] : 15;   //一共出多少套试卷
//        $num = !empty($paramArr['num']) ? $paramArr['num'] : 5; //每套卷子多少个题
//        $price = !empty($paramArr['price']) ? $paramArr['price'] : '1';
//        $des = !empty($paramArr['des']) ? $paramArr['des'] : '';    //试卷的描述
//
//
//        $title = !empty($paramArr['title']) ? $paramArr['title'] : '六年级英语下册-单词问答';
//        $type1 = !empty($paramArr['type1']) ? $paramArr['type1'] : '105';
//        $type2 = !empty($paramArr['type2']) ? $paramArr['type2'] : '115';
//
//        $star = !empty($paramArr['star']) ? $paramArr['star'] : 1;
//        $level = !empty($paramArr['level']) ? $paramArr['level'] : 62;

        if(!$title || !$price || !$type1 || !$type2){
            echo '缺少必要的参数，请进入程序查看';
            return;
        }

        if($num>10){
            echo '每套试卷最多10道题';
            return;
        }
        else{
//            echo '<pre>';
//            print_r($list);

            for($a=0; $a<$boxnum; $a++) {

                //0. 通过type1和type2获取已有的总试卷个数，然后给试卷标题后加编号
                $sql = "select count(*) as num from zj_qa_boxquestion where bq_type1=$type1 and bq_type2=$type2 and bq_title like '$title%' ";
                //echo $sql; exit;
                $rrr = Db::query($sql);
                $nowRow = $rrr[0]['num'];

                $trueTitle = $title . ($nowRow+1);

                $list = Db::name('qa_word')->where(['w_star'=>$star, 'w_level'=>$level, 'w_unit'=>$unit])->orderRaw("RAND()")->limit($num*40)->select()->toArray();
                if(count($list)==0 || count($list)<5){
                    echo '未找到任何单词，或单词小于5个，无法组成试卷';
                    return;
                }
                //如果单词量大于10个，则每套试卷由10个单词组成
                if(count($list) > 10){
                    $num = 10;
                }

//                echo $trueTitle;
//                echo '<pre>';print_r($list); exit;

                //以下是大循环插入多少套试卷，每刷新一次，就会自由组合成新的试卷
                //2. 插入试卷
                $inArr11['bq_type1'] = $type1;   //1级分类id
                $inArr11['bq_type2'] = $type2;   //2级分类id
                $inArr11['bq_title'] = $trueTitle;
                $inArr11['bq_des'] = $des;
                $inArr11['bq_easy'] = $star;    //难易程度
                $inArr11['bq_num'] = $num; //试卷个数
                $inArr11['bq_price'] = $price;   //试卷价格
                $inArr11['wxapp_id'] = $this->wxappid;
                $inArr11['create_time'] = date('Y-m-d H:i:s', time());
                $inArr11['update_time'] = date('Y-m-d H:i:s', time());
                $boxId = Db::name('qa_boxquestion')->insertGetId($inArr11);

                for ($i = 0; $i < $num; $i++) {
                    $inArr22['pq_pid'] = $boxId;
                    $inArr22['pq_type1'] = $type1;
                    $inArr22['pq_type2'] = $type2;
                    $inArr22['pq_easy'] = $star;

                    // 第1个单词，$i=0
                    $wordObj = $list[$i];
                    $rightWord = $wordObj['w_word'];
                    $inArr22['pq_question'] = $wordObj['w_type'] . ' “' . $wordObj['w_cname'] . '” 的英语是哪个？';
                    $inArr22['pq_type'] = 1;    // 单选多选填空题

                    //3. 从库中随机取4个不同的单词cnt
                    $randArr = $this->getRandArr(count($list), 4);
                    $xxArr = array($list[$randArr[0]]['w_word'], $list[$randArr[1]]['w_word'], $list[$randArr[2]]['w_word'], $list[$randArr[3]]['w_word']);
                    //echo $trueTitle; print_r($xxArr); exit;
                    $key = array_search($rightWord, $xxArr);

                    $inArr22['pq_ask1'] = $xxArr[0];
                    $inArr22['pq_ask2'] = $xxArr[1];
                    $inArr22['pq_ask3'] = $xxArr[2];
                    $inArr22['pq_ask4'] = $xxArr[3];
                    //在随机的4个单词中找到了答案单词，则答案用此序号
                    if ($key !== FALSE) {
                        $inArr22['pq_right'] = $key+1;
                    }
                    //在随机的4个单词中没有找到答案单词，则设置正确单词
                    else{
                        $rightCnt = rand(1,4);
                        $inArr22['pq_ask'.$rightCnt] =$rightWord;
                        $inArr22['pq_right'] = $rightCnt;
                    }

                    $inArr22['wxapp_id'] = $this->wxappid;
                    $inArr22['create_time'] = date('Y-m-d H:i:s', time());
                    $inArr22['update_time'] = date('Y-m-d H:i:s', time());
                    $perAskId = Db::name('qa_perquestion')->insertGetId($inArr22);
                    echo '<br>------' . $boxId . '------' . $perAskId;
                }
            }
        }
    }














    private function zeTiHuan($zzz, $str, $oldStr, $num=0){
        $backStr = "";
        if($num==0){
            $newStr = preg_replace($zzz, $str, $oldStr);
        }
        else{
            $newStr = preg_replace($zzz, $str, $oldStr, 1);
        }
        return $newStr;
    }
    private function read_csv($file)
    {
        $data = null;
        if(!is_file($file)&&!file_exists($file))
        {
            die('读取失败，请检查文件路径');
        }
        $cvs_file = fopen($file,'r');
        $i = 0;
        while ($file_data = fgetcsv($cvs_file))
        {
            $i++;
            if($i==1)
            {
                //continue;
            }
            //if($file_data[0]!='')
            //{
            //iconv('gbk','utf-8',var_export($row,true)).';');
            //$data[$i] = $file_data;
            foreach($file_data as $key => $val){
                $file_data[$key] = iconv('gbk','utf-8', $val.'');
            }
            $data[$i] = $file_data;
            // $data[$i] = iconv('gbk','utf-8', $file_data.'');
            //}

        }
        fclose($cvs_file);
        return $data;
    }
    private function getRandArr($arrLegth, $needNum){
        $arr = array();
        for($i=0; $i<$arrLegth; $i++){
            $arr[] = $i;
        }

        $backArr = array();
        $nowArr = $arr;
        for($j=0; $j<$needNum; $j++){
            $cnt = rand(0, count($nowArr)-1);
            $backArr[] = $nowArr[$cnt];
            unset($nowArr[$cnt]);
            $nowArr = array_values($nowArr);
        }
        return $backArr;
    }
}
